<?php

namespace App\Http\Livewire\Archives;

use App\Models\Category;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

class Create extends Component
{
    use AuthorizesRequests;

    public $archive;
    public $title;
    public $content;
    public $category_id;
    public $categories;

    public function mount(Category $category)
    {
        $this->category_id = $category->id;
    }
    public function save()
    {
        $this->authorize('create', $this->archive);
        $this->validate([
            'title' => 'required|between:5,255',
            'content' => 'nullable|min:10,1024'
        ]);
        $this->archive->title = $this->title;
        $this->archive->content = $this->content;
        $this->archive->user_id = Auth::id();
        $this->archive->category_id = $this->category_id;
        $this->archive->save();
        session()->flash('message', __('请添加标签和图片.'));
        return redirect()->route('archives.edit', $this->archive);
    }

    public function render()
    {
        return view('livewire.archives.create');
    }
}
